package promsvc

import (
	corev1 "gluenet/internal/agent/api/core/v1"
	"gluenet/utils/rpc/v2/define"
	"io"
	"net/http"
)

func (svc *Service) PromQuery(promQuery define.PromQuery) corev1.CommonResp {
	promUrl := svc.promCfg.Api + "?query=" + promQuery.Query + "&time=" + promQuery.Time
	svc.logger.Debugf("query -> ", promUrl)

	res, err := http.Get(promUrl)
	if err != nil {
		svc.logger.Errorf(err.Error())
		return corev1.NewCommonFailureResp(err.Error())
	}
	bts, err := io.ReadAll(res.Body)
	if err != nil {
		svc.logger.Errorf(err.Error())
		return corev1.NewCommonFailureResp(err.Error())
	}
	return corev1.NewCommonSuccessRespWithData(bts)
}

func (svc *Service) PromQueryRange(promQueryRange define.PromQueryRange) corev1.CommonResp {
	promUrl := svc.promCfg.ApiRange + "?query=" + promQueryRange.Query + "&start=" + promQueryRange.Start + "&end=" + promQueryRange.End + "&step=" + promQueryRange.Step
	svc.logger.Debugf("query -> ", promUrl)

	res, err := http.Get(promUrl)
	if err != nil {
		svc.logger.Errorf(err.Error())
		return corev1.NewCommonFailureResp(err.Error())
	}
	bts, err := io.ReadAll(res.Body)
	if err != nil {
		svc.logger.Errorf(err.Error())
		return corev1.NewCommonFailureResp(err.Error())
	}
	return corev1.NewCommonSuccessRespWithData(bts)
}
